package cn.edu.hqu.cst.android.sharedpreferences;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    public static int MODE=MODE_PRIVATE;
    Button btnLogin;
    EditText etAccount,etPassword;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnLogin=findViewById(R.id.btn_login);
        etAccount=findViewById(R.id.account_input);
        etPassword=findViewById(R.id.password_input);
        btnLogin.setOnClickListener(this);
        sharedPreferences=getSharedPreferences("data",MODE);
        etAccount.setText(sharedPreferences.getString("number",null));
        etPassword.setText(sharedPreferences.getString("password",null));

    }

        @Override
        public void onClick (View view){
            String number = etAccount.getText().toString().trim();
            String password = etPassword.getText().toString().trim();
            editor = sharedPreferences.edit();
            editor.putString("number", number);
            editor.putString("password", password);
            editor.apply();
            Toast.makeText(this, "登录成功", Toast.LENGTH_LONG).show();
    }

}